package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> selectShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        //设置用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //根据用户id查询购物车
        List<ShoppingCart> list = shoppingCartMapper.selectShoppingCart(shoppingCart);

        return list;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void insertShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置用户Id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //判断shopping_cart表中是否有这条数据
        List<ShoppingCart> list = shoppingCartMapper.selectShoppingCart(shoppingCart);

        if (list != null && list.size() > 0){
            ShoppingCart sc = list.get(0);
            //如果有,在原基础上number+1,价格相加
            sc.setNumber(sc.getNumber()+1);
            shoppingCartMapper.update(sc);
        }else {
            //设置菜品份数为1份
            shoppingCart.setNumber(1);
            //如果没有,就添加
            //判断是菜品还是套餐
            if (shoppingCart.getDishId() != null){
                //如果是菜品,就从菜品表中查找并添加到shopping_cart表中
                Dish dish = dishMapper.select(shoppingCart.getDishId());
                BeanUtils.copyProperties(dish,shoppingCart);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setAmount(new BigDecimal(dish.getPrice().toString()));

                shoppingCartMapper.insertShoppingCart(shoppingCart);
            }
            if (shoppingCart.getSetmealId() != null){
                //如果是套餐,就从套餐表中查找并添加到shopping_cart表中
                Setmeal setmeal = setmealMapper.selectBySetmealId(shoppingCart.getSetmealId());
                BeanUtils.copyProperties(setmeal,shoppingCart);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setAmount(new BigDecimal(setmeal.getPrice().toString()));

                shoppingCartMapper.insertShoppingCart(shoppingCart);
            }
        }
    }

    /**
     * 清空购物车
     * @param
     * @return
     */
    @Override
    public void deleteAll() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectShoppingCart(shoppingCart).get(0);
        //判断,如果 number > 1 ,则每次 -1 ,做更新操作
        if (cart.getNumber() > 1){
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }
        //判断,如果 number = 1 ,则删除该商品
        else {
            shoppingCartMapper.delete(cart);
        }
    }
}
